package com.xinsoft.controller.basic;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.SysSupplierDTO;
import com.xinsoft.entity.dto.SysSupplierPageParam;
import com.xinsoft.entity.vo.SysSupplierVo;
import com.xinsoft.service.SysSupplierService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/system/supplier")
public class SysSupplierController {

    @Autowired
    private SysSupplierService sysSupplierService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody SysSupplierDTO sysSupplierDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysSupplierDTO);
        return Response.succeed(sysSupplierService.addSysSupplier(sysSupplierDTO));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  SysSupplierDTO sysSupplierDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysSupplierDTO);
        return Response.succeed(sysSupplierService.editSysSupplier(sysSupplierDTO));
    }

    /**
     * 获取供应商
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(sysSupplierService.getSysSupplier(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        sysSupplierService.del(ids);
        return Response.succeed();
    }

    /**
     * 启用
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/enable")
    public Response<?> enable(@RequestBody List<Integer> ids) {
        sysSupplierService.enable(ids);
        return Response.succeed();
    }

    /**
     * 禁用
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/disable")
    public Response<?> disable(@RequestBody List<Integer> ids) {
        sysSupplierService.disable(ids);
        return Response.succeed();
    }


    /**
     * 审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        sysSupplierService.audit(ids);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deAudit")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        sysSupplierService.deAudit(ids);
        return Response.succeed();
    }

    /**
     * 导入
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/import")
    @ResponseBody
    public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile, HttpServletResponse response) throws IOException {
        return Response.succeed(sysSupplierService.importExcel(multipartFile,response));
    }

    @ApiOperation("导出")
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") SysSupplierPageParam sysSupplierPageParam, HttpServletResponse response) throws Exception {
        List<SysSupplierVo> sysSupplierDTOList = sysSupplierService.findListByParam(sysSupplierPageParam);
        ExcelUtils.exportByEasypoi(sysSupplierDTOList, SysSupplierDTO.class, "供应商", "供应商", response, "供应商");
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody SysSupplierPageParam sysSupplierPageParam) {
        return Response.succeed(sysSupplierService.findPageListByParam(sysSupplierPageParam));
    }
}
